﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using FuncSystem;


namespace Strategys
{
    public class Div
    {

        static string h_l;
        static double step = 0.01;
        static int fl;

        static int num_div;
        static int divline;
        static string divstr;
        

        static private double[] Min(DataTable Candels, int start, int end)
        {
            double[] value;
            value = new double[2]; 

            value[0] = Data.GetTable(Candels, h_l, start) * fl;

            for (int i = start + 1; i <= end; i++)
            {
                if (value[0] > Data.GetTable(Candels, h_l, i) * fl)
                {
                    value[0] = Data.GetTable(Candels, h_l, i) * fl;
                    value[1] = i;
                }
            }
            return value;
        }

        static void DivFor(DataTable Candels, double PriceExt, double mg2e, double mlExt, int k)
        {

            double PriceCur, mg0cur, mg1cur, mg2ecur, mg3cur, mlcur;
            double[] PriceC = new double[2];
            string div_power = "", divlinestr = "";
            
            // PriceExt последняя анализируемая цена high или low

            for (int i = k; i < k + 40; i++)
            {
                PriceCur = Data.GetTable(Candels, h_l, i) * fl;        // предидущая цена
                if (PriceExt > PriceCur & i > k + 3)        // если была цена еще ниже (для лонга) то выходим
                {
                    // выход
                    break;
                }
                
                // поиск след горки
                mg0cur = Data.GetTable(Candels, "MACDG", i) * fl;
                mg1cur = Data.GetTable(Candels, "MACDG", i + 1) * fl;
                mg2ecur = Data.GetTable(Candels, "MACDG", i + 2) * fl;
                mg3cur = Data.GetTable(Candels, "MACDG", i + 3) * fl;

                if (mg0cur > mg1cur & mg1cur > mg2ecur & mg2ecur < mg3cur)
                {
                    if (mg2e <= mg2ecur)
                    {
                        // выход горка большая, точнее идем дальше
                        continue;
                    }


                    PriceC = Div.Min(Candels, i, i + 3);
                    PriceCur = PriceC[0];
                    PriceCur = (Data.GetTable(Candels, "close", (int)(PriceC[1]) ) * fl + PriceCur ) / 2;

                    if (PriceExt <= PriceCur + 0 * step)
                    {

                        // есть дива
                        num_div++;
                        div_power = Convert.ToString(Math.Round((1 - mg2e / mg2ecur) * 100)) + "% ";
                        // проверяем на диву линий
                        mlcur = Math.Min(Data.GetTable(Candels, "MACD", i - 1) * fl, Data.GetTable(Candels, "MACD", i) * fl) * fl;
                        mlcur = Math.Min(mlcur * fl, Data.GetTable(Candels, "MACD", i + 1) * fl) * fl;
                        mlcur = Math.Min(mlcur * fl, Data.GetTable(Candels, "MACD", i + 2) * fl) * fl;
                        mlcur = mlcur * fl;

                        if (mlExt > mlcur)
                        {
                            divline++;
                            divlinestr = " линий " + Convert.ToString(Math.Round((1 - mlExt / mlcur) * 100)) + "%";
                        }
                        divstr += "(" + div_power + divlinestr + " от " + Data.GetTable(Candels,"date",i) + ") ";

                        if (num_div >= 3)
                        {
                            break;
                        }
                        // еще запуск
                        Div.DivFor(Candels, PriceCur, mg2ecur, mlcur, i + 2);
                        break;

                    }
                }
            }



        }

        public static DataTable DivMACD(DataTable Candels, DataTable Signal, string long_short, int k)
        {
            double mg0, mg1, mg2e, mg3, mg4, PriceExt, mlExt = 0.0, force = 0;
            string num_div_str, comment, timeframe, s_name = "new_div_macd";
            double[] PriceC = new double[2];
            num_div = 0;
            divline = 0;
            divstr = "";
            
            if (long_short == "лонг")
            {
                h_l = "low";
                fl = 1;
            }
            else
            {
                h_l = "high";
                fl = -1;
            }

            mg0 = Data.GetTable(Candels, "MACDG", k) * fl;
            mg1 = Data.GetTable(Candels, "MACDG", k + 1) * fl;
            mg2e = Data.GetTable(Candels, "MACDG", k + 2) * fl;
            mg3 = Data.GetTable(Candels, "MACDG", k + 3) * fl;
            timeframe = Data.GetField(Candels, "per", k);

            if (mg0 > mg1 & mg1 > mg2e & mg2e < mg3)
            {
                if (mg1 == mg2e)
                {
                    // ждем 25-ю минуту 0 свечи
                }

                PriceC = Div.Min(Candels, k, k + 3);
                PriceExt = PriceC[0];

                mlExt = Math.Min(Data.GetTable(Candels, "MACD", k) * fl, Data.GetTable(Candels, "MACD", k + 1) * fl) * fl;
                mlExt = Math.Min(mlExt * fl, Data.GetTable(Candels, "MACD", k + 2) * fl) * fl;
                mlExt = Math.Min(mlExt * fl, Data.GetTable(Candels, "MACD", k + 3) * fl) * fl;
                mlExt = mlExt * fl;

                Div.DivFor(Candels, PriceExt, mg2e, mlExt, 2);

            }

            // начинается тело программы
            //Candels.Columns.Add("comment", typeof(string));
            //DataRow[] myRows;
            //myRows = Candels.Select();
            //DataTable ResTable = Candels.Clone();
            //ResTable.Columns.Remove("emal");
            //ResTable.Columns.Remove("emas");
            //ResTable.Columns.Remove("low");
            //ResTable.Columns.Remove("macd");
            //ResTable.Columns.Remove("macdg");
            //ResTable.Columns.Remove("macds");

            if (num_div != 0)
            {
                // проверка на неявный пик
                mg4 = Data.GetTable(Candels, "MACDG", k + 4) * fl;
                if (mg3 > mg4 & mg2e >= mg4 & mg1 > mg3)
                {
                    if (num_div == 1 & timeframe != "1440") { num_div = 0; }
                }
            }

            if (num_div != 0)
            {
                if (divline != 0) { force = 1; }
                if (num_div == 1)
                {
                    num_div_str = "";

                }
                else
                {
                    num_div_str = num_div + "-ая";
                    force = 2;
                }

    
               comment = "Дива " + long_short + " " + num_div_str + ", сила " + divstr;
               if (timeframe == "1440") 
               {
                   s_name += "day";
               }
               else
               {
                   s_name += timeframe;
               }
               s_name += "_" + force;

               
               //myRows[k]["comment"] = comment;


               Signal.Rows.Add(Data.GetField(Candels, "ticker", k), Data.GetField(Candels, "date", k), Data.GetField(Candels, "time", k), s_name.ToUpper(), fl, force, Data.GetTable(Candels, "close", k), comment, timeframe, "div");

                 
            }


            return Signal; // возвращаем таблицу
            //return Candels;
            
        }

        public static DataTable TriMon(DataTable Candels)
        {
            // начинается тело программы
           

            return Candels; // возвращаем таблицу

        }


    }
}
